﻿using Smart.Hotfix.TABLE;
using Smart.Model;
using Smart.Model.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

namespace Smart.Hotfix
{
    [ObjectSystem]
    public class TableComponentAwakeSystem : AwakeSystem<TableComponent>
    {
        public override void Awake(TableComponent self)
        {
            self.Awake();
        }
    }

    public class TableComponent : Component
    {
        Dictionary<string,TableData> mTables;

        public void Awake()
        {

        }

        public void LoadAllTables()
        {
            mTables = Model.Entry.Instance.TableComponent.GetLoadedTables();
        }

        public TableData GetTable<T>() where T : ITable,new()
        {
            var name = typeof(T).Name;
            if(mTables.ContainsKey(name))
            {
                return mTables[name];
            }
            return null;
        }

        public T GetTableItem<T>(int id) where T : ITable, new()
        {
            var name = typeof(T).Name;
            if (mTables.ContainsKey(name))
            {
                var tableData = mTables[name];
                if(null != tableData && tableData.id2offset.ContainsKey(id))
                {
                    if(tableData.id2offset[id].Value is T table)
                    {
                        return table;
                    }
                }
            }
            return default(T);
        }

        public override void Dispose()
        {
            if (Disposed)
            {
                return;
            }
            if(null != mTables)
            {
                mTables.Clear();
                mTables = null;
            }
            base.Dispose();
        }
    }
}